//
//  ZJSCQusetionsTableViewCell.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/6.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJSCQusetionsTableViewCell.h"
#import "NSString+QT.h"
@interface ZJSCQusetionsTableViewCell ()
/** 内容 */
@property (nonatomic, strong) UILabel *contentLabel;
/** 时间 */
@property (nonatomic, strong) UILabel *time;
/** 背景View */
@property (nonatomic, strong) UIView *bgView;
/** 点赞 */
@property (nonatomic, strong) UIButton *like;
/** 下划线 */
@property (nonatomic, strong) UILabel *underline;

@end

@implementation ZJSCQusetionsTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.bgView];
        [self.contentView addSubview:self.contentLabel];
        [self.contentView addSubview:self.time];
        [self.contentView addSubview:self.like];
        [self.contentView addSubview:self.underline];
        
        self.contentLabel.sd_layout
        .topSpaceToView(self.contentView, 12 + [UIScreen zj_screenHScale])
        .leftSpaceToView(self.contentView, 72 * [UIScreen zj_screenWScale])
        .widthIs([UIScreen zj_screenWith] - (72 + 24) * [UIScreen zj_screenWScale])
        .heightIs(10);
        
        self.bgView.sd_layout
        .topSpaceToView(self.contentView, 0)
        .leftSpaceToView(self.contentView, 60 * [UIScreen zj_screenWScale])
        .rightSpaceToView(self.contentView, 12 * [UIScreen zj_screenWScale])
        .bottomSpaceToView(self.contentView, 0);
        
        self.time.sd_layout
        .topSpaceToView(self.contentLabel, 0)
        .leftEqualToView(self.contentLabel)
        .bottomEqualToView(self.bgView)
        .widthIs(220 * [UIScreen zj_screenWScale]);
        
        
        self.like.sd_layout
        .topEqualToView(self.time)
        .bottomEqualToView(self.time)
        .rightSpaceToView(self.contentView, 21 * [UIScreen zj_screenWScale])
        .leftSpaceToView(self.time, 0);
        
        self.underline.sd_layout
        .bottomSpaceToView(self.contentView, 0)
        .leftEqualToView(self.bgView)
        .rightSpaceToView(self.contentView, 0)
        .heightIs(1);
        
        
    }
    return self;
}







#pragma mark - 点击事件
- (void)action_like:(UIButton *)like{
    if (like.selected) {
        return;
    }
    __weak typeof(self) weakSelf = self;
    [self netCollectWithString:@"0" success:^{
        like.selected = YES;
        weakSelf.model.PRAISECOUNT = [NSString stringWithFormat:@"% ld",[weakSelf.model.PRAISECOUNT integerValue] + 1];
        [like setTitle:weakSelf.model.PRAISECOUNT forState:UIControlStateNormal];
        weakSelf.model.ISCLICK = @"0";
        if (weakSelf.delegate) {
            [weakSelf.delegate ZJSCQusetionsTableViewCellDelegate_operateSuccessToModel:weakSelf.model indexPatch:weakSelf.indexPatch];
        }
    }];
}

//EVENTTYPE":0 点赞 1 关注
- (void)netCollectWithString:(NSString *)string success:(void(^)())success{
    NSDictionary *dict = @{@"STUDENTID":ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,@"EVENTID":self.model.ID,@"EVENTTYPE":string};
    [NetWorkingManager postWithUrlString:[NetWorkingURL praiseOrAttention] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//成功
            success();
        }
    } Fail:^(id object) {
        
    }];
}




#pragma mark - Setter
- (void)setModel:(ZJSCQusetionsCellModel *)model{
    _model = model;
    NSString *contentText = [NSString stringWithFormat:@"%@ : %@",model.USER_NAME,model.CONTENT];
    self.contentLabel.keywordsArray = @[[NSString stringWithFormat:@"%@ : ",model.USER_NAME]];
    self.contentLabel.keywordsColor = [UIColor zj_colorWithHexString:@"0098e1"];
    self.contentLabel.text = contentText;

    self.contentLabel.sd_layout
    .topSpaceToView(self.contentView, 12 + [UIScreen zj_screenHScale])
    .leftSpaceToView(self.contentView, 72 * [UIScreen zj_screenWScale])
    .widthIs([UIScreen zj_screenWith] - (72 + 24) * [UIScreen zj_screenWScale])
    .heightIs([self.contentLabel getLableSizeWithMaxWidth:self.contentLabel.width].height);
    
    [self.contentLabel updateLayout];
    self.time.text = model.CREATETIME;
    
    [self.like setTitle:[NSString stringWithFormat:@" %@", model.PRAISECOUNT] forState:UIControlStateNormal];
    
    //判断收藏。点赞情况(0点赞，1关注，2未点赞且未关注  3点赞且关注)
    if ([model.ISCLICK isEqualToString:@"0"]) {
        self.like.selected = YES;
    }else{
        self.like.selected = NO;
    }
}

//是否是最后一个cell
- (void)setIsLastCell:(BOOL)isLastCell{
    
    if (isLastCell) {//是最后一个cell
        self.underline.hidden = NO;
        
        self.time.sd_layout
        .topSpaceToView(self.contentLabel, 0)
        .leftEqualToView(self.contentLabel)
        .bottomSpaceToView(self.underline, 20 * [UIScreen zj_screenHScale])
        .widthIs(220 * [UIScreen zj_screenWScale]);
        
        self.bgView.sd_layout
        .topSpaceToView(self.contentView, 0)
        .leftSpaceToView(self.contentView, 60 * [UIScreen zj_screenWScale])
        .rightSpaceToView(self.contentView, 12 * [UIScreen zj_screenWScale])
        .bottomSpaceToView(self.underline, 12 * [UIScreen zj_screenHScale]);
    }else{
        
        self.underline.hidden = YES;
        self.time.sd_layout
        .topSpaceToView(self.contentLabel, 0)
        .leftEqualToView(self.contentLabel)
        .bottomSpaceToView(self.contentView, 0)
        .widthIs(220 * [UIScreen zj_screenWScale]);
        
        self.bgView.sd_layout
        .topSpaceToView(self.contentView, 0)
        .leftSpaceToView(self.contentView, 60 * [UIScreen zj_screenWScale])
        .rightSpaceToView(self.contentView, 12 * [UIScreen zj_screenWScale])
        .bottomSpaceToView(self.contentView, 0);
    }
    [self.time updateLayout];
    [self.bgView updateLayout];
    
}

#pragma mark - Getters
- (UIView *)bgView{
    if (_bgView == nil){
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor zj_colorWithHexString:@"f3f3f3"];
    }
    return _bgView;
}

- (UILabel *)contentLabel{
    if (_contentLabel == nil){
        _contentLabel = [UILabel new];
        _contentLabel.font = [UIFont titleTextFont];
        _contentLabel.textColor = [UIColor textBlackColor];
        _contentLabel.numberOfLines = 0;
        _contentLabel.lineSpace = 4 * [UIScreen zj_screenWScale];
        _contentLabel.characterSpace = 1;
    }
    return _contentLabel;
}

- (UILabel *)time{
    if (_time == nil){
        _time = [UILabel new];
        _time.textColor = [UIColor zj_colorWithHexString:@"a8a8a8"];
        _time.font = [UIFont titleTextFont];
    
    }
    return _time;
}

- (UIButton *)like{
    if (_like == nil){
        _like = ({
            UIButton *button = [UIButton new];
            [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
            button.titleLabel.font = [UIFont subTitleTextFont];
            [button setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"learn_question_shoucang_d"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"learn_question_shoucang_h"] forState:UIControlStateSelected];
            [button addTarget:self action:@selector(action_like:) forControlEvents:UIControlEventTouchUpInside];

            button;
        });
    }
    return _like;
}
- (UILabel *)underline{
    if (_underline == nil){
        _underline = [UILabel new];
        _underline.backgroundColor = [UIColor zj_colorWithHexString:@"ededed"];
    }
    return _underline;
}

@end
